package cc.mrbird.febs.accounting.service.impl;

import cc.mrbird.febs.accounting.entity.Company;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.accounting.entity.DetailSubject;
import cc.mrbird.febs.accounting.mapper.DetailSubjectMapper;
import cc.mrbird.febs.accounting.service.IDetailSubjectService;
import cc.mrbird.febs.common.entity.Strings;
import cc.mrbird.febs.common.utils.FebsUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import lombok.RequiredArgsConstructor;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 *  Service实现
 *
 * @author baoyinlei
 * @date 2021-10-11 21:37:40
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class DetailSubjectServiceImpl extends ServiceImpl<DetailSubjectMapper, DetailSubject> implements IDetailSubjectService {

    private final DetailSubjectMapper detailSubjectMapper;

    @Override
    public IPage<DetailSubject> findDetailSubjects(QueryRequest request, DetailSubject detailSubject) {
        LambdaQueryWrapper<DetailSubject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DetailSubject::getProjectId, detailSubject.getProjectId());
        queryWrapper.eq(DetailSubject::getIsDeleted, 0);
        if (detailSubject.getMainSubjectId() != null) {
            queryWrapper.eq(DetailSubject::getSubjectId, detailSubject.getMainSubjectId());
        }
        Page<DetailSubject> page = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(page, queryWrapper);
    }

    @Override
    public List<DetailSubject> findDetailSubjects(DetailSubject detailSubject) {
	    LambdaQueryWrapper<DetailSubject> queryWrapper = new LambdaQueryWrapper<>();
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createDetailSubject(DetailSubject detailSubject) {
        detailSubject.setDetailSubjectCode(UUID.randomUUID().toString());
        detailSubject.setCreateBy(FebsUtil.getCurrentUser().getUsername());
        this.save(detailSubject);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDetailSubject(DetailSubject detailSubject) {
        detailSubject.setModifyTime(new Date());
        detailSubject.setModifyBy(FebsUtil.getCurrentUser().getUsername());
        this.saveOrUpdate(detailSubject);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteDetailSubject(String detailSubjectIds) {
        List<String> list = Arrays.asList(detailSubjectIds.split(Strings.COMMA));
        list.forEach(e -> {
            DetailSubject detailSubject = new DetailSubject();
            detailSubject.setId(Long.parseLong(e));
            detailSubject.setIsDeleted(1);
            detailSubject.setModifyBy(FebsUtil.getCurrentUser().getUsername());
            detailSubject.setModifyTime(new Date());
            this.baseMapper.updateById(detailSubject);
        });
	}

    @Override
    public List<DetailSubject> queryByProject(Long projectId, Long subjectId) {
        LambdaQueryWrapper<DetailSubject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DetailSubject::getProjectId, projectId);
        queryWrapper.eq(DetailSubject::getIsDeleted, 0);
        queryWrapper.eq(DetailSubject::getSubjectId, subjectId);
        return this.baseMapper.selectList(queryWrapper);
    }
}
